package cn.smbms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;

@Controller
@RequestMapping(value="/sys/provider")
public class ProviderController {
	@Resource
	private ProviderService providerService; 
	//查询所有供应商
	@RequestMapping(value="/providerlist")
	public String getAllProvider(HttpServletRequest request,Model model,Provider provider,
			 @RequestParam(value="queryProCode",required=false)String queryProCode,
			 @RequestParam(value="queryProName",required=false)String queryProName,
			 @RequestParam(value="queryCreatedBy",required=false)String queryCreatedBy,
			 @RequestParam(value="pageIndex",required=false)String pageIndex) {
		//设置页面容量
		int pageSize=Constants.pageSize;
		//当前页面
		int currentPageNo=1;
		
		
		 int queryCreatBy=0;
		 if(queryProName==null){
			 queryProName="";
		 }
		 if(queryProCode==null) {
			 queryProCode="";
		 }
		 if(queryCreatedBy!=null&&!queryCreatedBy.equals("")){
			 queryCreatBy=Integer.parseInt(queryCreatedBy);
		 }
		 if(pageIndex!=null){
			 try {
				 currentPageNo=Integer.valueOf(pageIndex);
			} catch (Exception e) {
				return "redirect:/sys/providerlist";
			}
		 }
		 int totalCount=providerService.countProvider(queryProCode, queryProName);
		 PageSupport pages=new PageSupport();
		 pages.setCurrentPageNo(currentPageNo);
		 pages.setPageSize(pageSize);
		 pages.setTotalCount(totalCount);
		 int totalPageCount=pages.getTotalCount();
		 if(currentPageNo<1){
			 currentPageNo=1;
		 }else if(currentPageNo>totalPageCount){
			 currentPageNo=totalPageCount;
		 }
		 int start=(currentPageNo-1)*pageSize;
		 List<Provider> providerList=providerService.getProviderList(queryProCode, queryProName, pageSize, start);
			request.setAttribute("providerList", providerList);
		 model.addAttribute("queryProCode", queryProCode);
		 model.addAttribute("queryProName", queryProName);
		 model.addAttribute("queryCreatedBy", queryCreatedBy);
		 model.addAttribute("pageIndex", pageIndex);
		 model.addAttribute("totalPageCount", totalPageCount);
		 model.addAttribute("totalCount", totalCount);
		 model.addAttribute("currentPageNo", currentPageNo);
		return "providerlist";
	}
	//添加供应商
	@RequestMapping(value="/provideradd",method=RequestMethod.GET)
	public String add() {
		return "provideradd";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addProvider(Provider provider,HttpServletRequest reques,HttpSession session) {
		provider.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		provider.setCreationDate(new Date());
		int i=providerService.addProvider(provider);
		if(i>0) {
			return "redirect:/sys/provider/providerlist";
		}else {
			return "provideradd";
			
		}
	}
	//修改供应商
	@RequestMapping(value="providermodify/{id}",method=RequestMethod.GET)
	public String updateProvider(@PathVariable("id") int id,HttpServletRequest request,HttpSession session) {
		Provider provider=providerService.getProviderById(id);
		request.setAttribute("provider",provider );
		return "providermodify";
	}
	@RequestMapping(value="providermodify",method=RequestMethod.POST)
	public String doUpdateProvider(Provider provider,HttpSession session) {
		provider.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		provider.setModifyDate(new Date());
		int u=providerService.updateProvider(provider);
		if(u>0) {
			return "redirect:/sys/provider/providerlist";
		}else {
			return "providermodify";
		}		
	}
	//查看供应商详细信息
	@RequestMapping(value="/providerview/{id}",method=RequestMethod.GET)
	public String view(@PathVariable int id,Model model) {
		Provider provider=providerService.getProviderById(id);
		model.addAttribute(provider);
		return "providerview";
	}
	//删除供应商
	@RequestMapping(value="/del.json",method=RequestMethod.GET)
	@ResponseBody
	public Object delprovider(@RequestParam(value="proid") String id){
		
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(id)){
			boolean flag = false;
			//boolean lag = false;
			try {
				flag = providerService.deleteProvider(Integer.parseInt(id));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(flag==true){//删除成功
				resultMap.put("delResult", "true");
			}else{//删除失败
				resultMap.put("delResult", "false");
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		return JSONArray.toJSONString(resultMap);
	}
	
}
